<!DOCTYPE html>
<html lang="en">
<head>
    <title>预测结果记录</title>
    {% include 'admin/common/header.html' %}
    <link rel="stylesheet" href="{{ url_for('static', filename='admin/admin/css/other/user.css') }}"/>
</head>
<body class="pear-container">
{# 查询表单 #}
{% if role_id != 3 %}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="record-query-form">
            <div class="layui-form-item">
                <label class="layui-form-label">所属医院</label>
                <div class="layui-input-inline">
                    <select name="hospital_no">
                        <option value="">请选择</option>
                        {% for hospital in hospital_list %}
                            <option value="{{ hospital.hospital_no }}" {% if hospital.hospital_no == hospital_no %}selected{% endif %}>{{ hospital.name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <label class="layui-form-label">检测设备</label>
                <div class="layui-input-inline">
                    <select name="device_no">
                        <option value="">请选择</option>
                        {% for device in device_list %}
                            <option value="{{ device.device_no }}" {% if device.device_no == device_no %}selected{% endif %}>{{ device.name }}</option>
                        {% endfor %}
                    </select>
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="record-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
{% endif %}
{# 用户表格 #}
<div class="user-main user-collasped">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="record-table" lay-filter="record-table"></table>
        </div>
    </div>
</div>
</body>
{# 表格操作 #}
<script type="text/html" id="record-toolbar">
    {% if authorize("admin:record:add") %}
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="pear-icon pear-icon-add"></i>
            新增
        </button>
    {% endif %}
    {% if authorize("admin:record:r_download") %}
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="r_downloads">
            <i class="pear-icon pear-icon-add"></i>
            批量下载
        </button>
    {% endif %}
    {% if authorize("admin:record:remove") %}
        <button class="pear-btn pear-btn-md" lay-event="batchRemove">
            <i class="pear-icon pear-icon-ashbin"></i>
            删除
        </button>
    {% endif %}
</script>

{# 用户修改操作 #}
<script type="text/html" id="record-bar">
    {% if authorize("admin:record:edit") %}
        {% raw %}
            {{#  if(d.results){ }}
            <a class="layui-btn layui-btn-sm" data-title="结果查看" lay-event="info">结果查看</a>
            <a class="layui-btn layui-btn-sm" data-title="结果查看" lay-event="r_download">结果下载</a>
            {{#  }else{ }}
            <a class="layui-btn layui-btn-sm" data-title="编辑标签数据" lay-event="edit">样本核查</a>
            {{#  } }}
        {% endraw %}
    {% endif %}
    {% if authorize("admin:record:remove") %}
        {% raw %}
        {{#  if(!d.results){ }}
        <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="remove">删 除</a>
        {{#  } }}
        {% endraw %}
    {% endif %}
</script>


{# 用户注册时间 #}
<script type="text/html" id="record-createTime">
    {{ '  {{layui.util.toDateString(d.create_time,  "yyyy-MM-dd HH:mm")}' |safe }}}
</script>

{% include 'admin/common/footer.html' %}

<script>
    layui.use(['table', 'form', 'jquery', 'popup', 'common'], function () {
        let table = layui.table
        let form = layui.form
        let $ = layui.jquery
        let popup = layui.popup
        let MODULE_PATH = '/admin/record/'

        // 表格数据
        let cols = [
            [
                {% if authorize("admin:record:remove") %}
                    {type: 'checkbox'},
                {% endif %}
                {title: '医院名称', field: 'hospital', align: 'center', width: 110},
                {title: '设备名称', field: 'device', align: 'center'},
                {title: '样本数量', field: 'cc', align: 'center'},
                {title: '创建时间', field: 'create_time', templet: '#record-createTime', align: 'center'},
                {title: '操作', toolbar: '#record-bar', align: 'center', width: 200}
            ]
        ]

        // 渲染表格数据
        table.render({
            elem: '#record-table',
            url: MODULE_PATH + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            height: 'full-148',
            toolbar: '#record-toolbar', /*工具栏*/
            text: {none: '暂无相关信息'},
            defaultToolbar: [{layEvent: 'refresh', icon: 'layui-icon-refresh'}, 'filter'], /*默认工具栏*/
            done: function (a, b, c) {
                console.log(a,b,c)
            }
        })


        table.on('tool(record-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj)
            } else if (obj.event === 'edit') {
                window.edit(obj)
            } else if (obj.event === 'info') {
                window.info(obj)
            } else if (obj.event === 'r_download') {
                window.r_download(obj)
            }
        })

        table.on('toolbar(record-table)', function (obj) {
            console.log(obj.event);
            if (obj.event === 'add') {
                window.add()
            } else if (obj.event === 'refresh') {
                window.refresh()
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj)
            }
            if (obj.event === 'r_downloads') {
                window.r_downloads(obj)
            }
        })

        form.on('submit(record-query)', function (data) {
            window.refresh(data.field)
            return false
        })

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['550px', '550px'],
                content: MODULE_PATH + 'add'
            })
        }

        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '样本核查',
                shade: 0.1,
                area: ['100%', '100%'],
                content: MODULE_PATH + 'edit/' + obj.data['random']
            })
        }

        window.r_download = function (obj) {
            window.location.href = MODULE_PATH + 'r_download/' + obj.data['random']
        }

        window.r_downloads = function (obj) {
            let data = table.checkStatus(obj.config.id).data
            if (data.length === 0) {
                layer.msg('未选中数据', {
                    icon: 3,
                    time: 1000
                })
                return false
            }
            var randoms = []
            var hasCheck = table.checkStatus('record-table')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    randoms.push(element.random)
                })
            }
            console.log(randoms)
            window.location.href = MODULE_PATH + 'r_downloads/' + randoms.join(",")

        }

        window.info = function (obj) {
            layer.open({
                type: 2,
                title: '结果查看',
                shade: 0.1,
                area: ['1100px', '600px'],
                content: MODULE_PATH + 'info/' + obj.data['random']
            })
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除', {icon: 3, title: '提示'}, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({
                    url: MODULE_PATH + 'remove/' + obj.data['random'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            popup.success(result.msg, function () {
                                obj.del()
                            })
                        } else {
                            popup.failure(result.msg)
                        }
                    }
                })
            })
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data
            if (data.length === 0) {
                layer.msg('未选中数据', {
                    icon: 3,
                    time: 1000
                })
                return false
            }
            var randoms = []
            var hasCheck = table.checkStatus('record-table')
            var hasCheckData = hasCheck.data
            if (hasCheckData.length > 0) {
                $.each(hasCheckData, function (index, element) {
                    randoms.push(element.random)
                })
            }
            {#console.log(ids);#}
            layer.confirm('确定要删除选中数据', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index)
                let loading = layer.load()
                $.ajax({

                    url: MODULE_PATH + 'batchRemove',
                    data: {randoms: randoms},
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading)
                        if (result.success) {
                            popup.success(result.msg, function () {
                                table.reload('record-table')
                            })
                        } else {
                            popup.failure(result.msg)
                        }
                    }
                })
            })
        }

        window.refresh = function (param) {
            table.reload('record-table', {where: param})
        }
    })
</script>
</html>